package com.yowasa.xiuxian.engine.message.hone;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.battle.strategy.BattleStrategy;
import com.yowasa.xiuxian.engine.battle.strategy.BattleStrategyEngine;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.MessageFormat;
import java.util.Set;
import java.util.stream.Collectors;

@CMD(name = "/学习战技", scope = CmdScopeEnum.ALL)
public class StudyBattleHandler extends CmdMessageHandler {
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;

    @Override
    public void execute(SendMessage message) {
        UserBO user = userHelper.checkOpUserAll(message);
        String select = getParam(message);
        Set<String> allStrategy = BattleStrategyEngine.strategyMap.keySet();
        if (user.getUserConfig().getStudiedStrategies().contains(select)) {
            throw new HandlerException("你已经学习过此战技，无需再次学习");
        }
        if (StringUtils.isBlank(select)) {
            messageHelper.replyMys(message, "可学习的战技有:\n" + user.getUserConfig().getCanStudyStrategies().stream().collect(Collectors.joining("\n")));
            return;
        }
        if (!allStrategy.contains(select)) {
            throw new HandlerException("未识别的战技,请先/设置 查看");
        }
        if (!user.getUserConfig().getCanStudyStrategies().contains(select)) {
            throw new HandlerException("你未获得过该战技的秘籍");
        }
        BattleStrategy battleStrategy = BattleStrategyEngine.strategyMap.get(select);
        if (user.getUserConfig().getStudiedSpecialEffect().contains("太玄妙法") && user.getUserAttr().getUnd() <= 30) {
        } else {
            battleStrategy.limit(user);
        }
        int need = battleStrategy.calNeedTravel(user);
        int upAbility = battleStrategy.userUpAbility(user);
        int travel = user.getUserConfig().getTravel();
        if (travel < need) {
            throw new HandlerException(MessageFormat.format("你的历练值不够，无法学习[{0}/{1}]", travel, need));
        }
        user.getUserConfig().setTravel(travel - need);
        user.getUserConfig().setAbility(user.getUserConfig().getAbility() + upAbility);
        user.getUserConfig().getStudiedStrategies().add(select);

        if (user.getUserConfig().getBattleStrategyList().size() < user.getUserConfig().getStrategyMax()) {
            user.getUserConfig().getBattleStrategyList().add(select);
        }

        user.getUserConfig().getCanStudyStrategies().remove(select);
        userHelper.saveUserInfo(user);
        messageHelper.replyMys(message, MessageFormat.format("你消耗了{0}点历练值习得了{1}", need, select));
    }

    public String desc(){
        return new StringBuffer().append("用于学习战技的指令，/学习战技 战技名称\n")
                .append("不带战技名称会展示所有当前可学习的战技\n")
                .append("学习战技需要历练值，学习后会增加道行，道行越高，学习战技所需历练值也越高")
                .toString();

    }
}
